package nl.hr.minor.mobilelife.j1213.jlr.flow;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.util.Log;

/**
 * A level in the Flow game. Hosts and manages all current level related stuff. Uses {@link ILevelToPanelCallbacks}
 * to communicate with the {@link FlowPanel}.
 * All levels are managed by the {@link LevelManager}.
 * @author Leon
 *
 */
public class FlowLevel {

	public int levelCode;
	public int rowcolCount;
	private HostBlock[][] hostBlocks;
	
	private static final int COLOR_RED = 1;
	private static final int COLOR_ORANGE = 2;
	private static final int COLOR_YELLOW = 3;

	private static final int COLOR_GREEN = 5;
	private static final int COLOR_DARK_GREEN = 6;
	private static final int COLOR_LIGHT_BLUE = 7;
	private static final int COLOR_BLUE = 8;
	private static final int COLOR_DARK_BLUE = 9;
	private static final int COLOR_MAGENTA = 10;

	public FlowLevel(int levelCode, JSONObject json) {
		this.levelCode = levelCode;
		try {
			if(json == null) return;

			if(json.has("rowcolcount") && json.has("hostblocks")) {

				rowcolCount = json.getInt("rowcolcount");

				JSONArray hostblocks = json.getJSONArray("hostblocks");
				int length = hostblocks.length();
				
				hostBlocks = new HostBlock[length][2];

				int startRow = 0, startCol = 0, endRow = 0, endCol = 0, colorCode = 0;
				JSONObject hostblockFromJSON = null;
				for(int i = 0; i < length; i++ ) {

					hostblockFromJSON = hostblocks.getJSONObject(i);

					startRow = hostblockFromJSON.getInt("startRow");
					startCol = hostblockFromJSON.getInt("startCol");
					endRow = hostblockFromJSON.getInt("endRow");
					endCol = hostblockFromJSON.getInt("endCol");
					colorCode = hostblockFromJSON.getInt("colorcode");

					hostBlocks[i] = new HostBlock[2];
					hostBlocks[i][0] = new HostBlock(startRow, startCol);
					hostBlocks[i][1] = new HostBlock(endRow, endCol);
					
					hostBlocks[i][0].setColor(FlowLevel.localColorCodeToFrameworkColor(colorCode));
					hostBlocks[i][1].setColor(FlowLevel.localColorCodeToFrameworkColor(colorCode));

					// clean up for the next loop
					startRow = 0; startCol = 0; endRow = 0; endCol = 0;
					hostblockFromJSON = null;
				}

			} else {
				Log.i("FlowLevel", "Could not parse level, 'rowcolcount' or 'hostblocks' array not found");
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public HostBlock[][] getHostBlocks() {
		return this.hostBlocks;
	}

	private static int localColorCodeToFrameworkColor(int localColorCode) {
		switch(localColorCode) {
		case COLOR_RED : return Color.RED;
		case COLOR_ORANGE : return Color.rgb(255, 127, 0);
		case COLOR_YELLOW : return Color.YELLOW;
		case COLOR_GREEN : return Color.GREEN;
		case COLOR_DARK_GREEN : return Color.rgb(00, 77, 00);
		case COLOR_LIGHT_BLUE : return Color.rgb(0, 255, 255);
		case COLOR_BLUE : return Color.BLUE;
		case COLOR_DARK_BLUE : return Color.rgb(111, 0, 255);
		case COLOR_MAGENTA : return Color.MAGENTA;
		default: return 0;
		}
	}
}
